<template>
  <div class="products-container">
    <div class="header">
      <h2>产品管理</h2>
      <el-button type="primary" @click="showAddDialog" class="add-button">添加产品</el-button>
    </div>

    <el-table v-loading="loading" :data="products" style="width: 100%">
      <el-table-column prop="name" label="产品名称" />
      <el-table-column prop="description" label="产品描述" />
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button size="small" @click="showEditDialog(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form :model="productForm" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="productForm.name" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            v-model="productForm.description"
            type="textarea"
            rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useScheduleStore } from '../stores/schedule.js'
import { ElMessage } from 'element-plus'

const store = useScheduleStore()
const loading = ref(false)
const dialogVisible = ref(false)
const dialogTitle = ref('')
const products = ref([])

const productForm = ref({
  name: '',
  description: ''
})

const resetForm = () => {
  productForm.value = {
    name: '',
    description: ''
  }
}

const showAddDialog = () => {
  dialogTitle.value = '添加产品'
  resetForm()
  dialogVisible.value = true
}

const showEditDialog = (row) => {
  dialogTitle.value = '编辑产品'
  productForm.value = { 
    ID: row.ID,
    name: row.name,
    description: row.description
  }
  dialogVisible.value = true
}

const handleSubmit = async () => {
  try {
    if (!productForm.value.name) {
      ElMessage.error('产品名称不能为空')
      return
    }
    
    if (dialogTitle.value === '添加产品') {
      await store.createProduct(productForm.value)
      ElMessage.success('添加成功')
    } else {
      await store.updateProduct(productForm.value)
      ElMessage.success('更新成功')
    }
    dialogVisible.value = false
    await fetchProducts()
  } catch (error) {
    console.error('操作失败:', error)
    ElMessage.error('操作失败: ' + (error.response?.data?.error || error.message))
  }
}

const handleDelete = async (row) => {
  try {
    if (!row || !row.ID) {
      ElMessage.error('无效的产品ID')
      return
    }
    
    await store.deleteProduct(row.ID)
    ElMessage.success('删除成功')
    await fetchProducts()
  } catch (error) {
    console.error('删除产品出错:', error)
    ElMessage.error('删除失败: ' + (error.response?.data?.error || error.message))
  }
}

const fetchProducts = async () => {
  loading.value = true
  try {
    await store.fetchProducts()
    products.value = store.products
  } catch (error) {
    ElMessage.error('获取产品列表失败')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  fetchProducts()
})
</script>

<style scoped>
.products-container {
  height: 100%;
  display: flex;
  flex-direction: column;
  max-width: 1600px;
  margin: 0 auto;
  width: 100%;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--apple-spacing-lg);
  flex-wrap: wrap;
  gap: var(--apple-spacing-sm);
}

.add-button {
  font-weight: 500;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--apple-spacing-sm);
}

/* 响应式调整 */
@media (max-width: 768px) {
  .header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--apple-spacing-sm);
    margin-bottom: var(--apple-spacing-md);
  }
  
  .header h2 {
    margin-bottom: var(--apple-spacing-xs);
  }
  
  .add-button {
    align-self: flex-start;
  }
}

@media (max-width: 480px) {
  .el-table {
    font-size: 13px;
  }
  
  .el-button.is-small {
    padding: 6px 10px;
    font-size: 12px;
  }
}
</style> 